<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="项目id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentId">
              <a-input v-model="model.equipmentId" placeholder="请输入项目id"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="零部件名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialId">
              <j-popup
                v-model="model.materialName"
                field="materialId"
                org-fields="id,material_name"
                dest-fields="materialId,materialName"
                code="materialQuery"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="零部件名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialName">
              <a-input v-model="model.materialName" placeholder="请输入零部件名称"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="零部件类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialType">
              <j-dict-select-tag type="list" v-model="model.materialType" dictCode="material_type" placeholder="请选择零部件类别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="配置人员角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="configRoles">
              <a-input v-model="model.configRoles" placeholder="请输入配置人员角色"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="零部件位号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="locationNumber">
              <a-input v-model="model.locationNumber" placeholder="请输入零部件位号"  ></a-input>
            </a-form-model-item>
          </a-col>
          
          <!-- <a-col :span="12">
            <a-form-model-item label="控件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlType">
              <a-input v-model="model.controlType" placeholder="请输入控件类型"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="控件值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <a-input v-model="model.controlValue" placeholder="请输入控件值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否必填" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlRequired">
              <a-input v-model="model.controlRequired" placeholder="请输入是否必填"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="12">
            <a-form-model-item label="有效期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="effectiveDate">
              <j-date placeholder="请选择有效期" v-model="model.effectiveDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="最大次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maximumTimes">
              <a-input-number v-model="model.maximumTimes" placeholder="请输入最大次数" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
                    <a-col :span="12">
            <a-form-model-item label="检查日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkDate">
              <j-date placeholder="请选择检查日期" v-model="model.checkDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="检查间隔(月)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkGap">
              <a-input-number v-model="model.checkGap" placeholder="请输入检查间隔(月)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="保养日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenanceDate">
              <j-date placeholder="请选择保养日期" v-model="model.maintenanceDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="检查间隔(月)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenanceGap">
              <a-input-number v-model="model.maintenanceGap" placeholder="请输入检查间隔(月)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="更换日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="changeDate">
              <j-date placeholder="请选择更换日期" v-model="model.changeDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="更换间隔(月)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="changeGap">
              <a-input-number v-model="model.changeGap" placeholder="请输入更换间隔(月)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"  ></a-textarea>
            </a-form-model-item>
          </a-col>   
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YksEquipmentConfigurationPartForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        action:'',
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 17 },
        },
        confirmLoading: false,
        currentEquipmentId:'',
        validatorRules: {
           materialId: [
              { required: true, message: '请输入零部件id!'},
           ],
           locationNumber: [
              { required: true, message: '请输入零部件位号!'},
           ],
        },
        url: {
          add: "/dev/yksEquipmentConfigurationPart/add",
          edit: "/dev/yksEquipmentConfigurationPart/edit",
          queryById: "/dev/yksEquipmentConfigurationPart/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.action = "add"
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (this.action == "add") {
           this.model.equipmentId = this.currentEquipmentId
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>